package com.example.suhui_test;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.ComponentActivity;

import com.example.suhui_test.customview.CircleProgressView;
import com.example.suhui_test.customview.DraggableCircleView;
import com.example.suhui_test.customview.CustomTextView;
import com.example.suhui_test.customview.FlowLayout;

/**
 * 自定义View示例Activity
 * 展示Android自定义View的各种实现方式和知识点
 */
public class CustomViewActivity extends ComponentActivity {
    private static final String TAG = "CustomViewActivity";
    
    private CircleProgressView circleProgressView;
    private DraggableCircleView draggableCircleView;
    private CustomTextView customTextView;
    private FlowLayout flowLayout;
    private SeekBar progressSeekBar;
    private TextView progressText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_view);
        
        initViews();
        setupListeners();
        addFlowLayoutItems();
    }

    /**
     * 初始化视图组件
     */
    private void initViews() {
        circleProgressView = findViewById(R.id.circle_progress_view);
        draggableCircleView = findViewById(R.id.draggable_circle_view);
        customTextView = findViewById(R.id.custom_text_view);
        flowLayout = findViewById(R.id.flow_layout);
        progressSeekBar = findViewById(R.id.progress_seekbar);
        progressText = findViewById(R.id.progress_text);
        
        // 设置初始进度
        circleProgressView.setProgress(30);
        progressSeekBar.setProgress(30);
        progressText.setText("进度: 30%");
    }

    /**
     * 设置事件监听器
     */
    private void setupListeners() {
        // 进度条控制
        progressSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    circleProgressView.setProgress(progress);
                    progressText.setText("进度: " + progress + "%");
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });

        // 可拖拽圆形View的位置监听
        draggableCircleView.setOnPositionChangeListener(new DraggableCircleView.OnPositionChangeListener() {
            @Override
            public void onPositionChanged(float x, float y) {
                // 可以在这里处理位置变化事件
                // Toast.makeText(CustomViewActivity.this, "位置: (" + (int)x + ", " + (int)y + ")", Toast.LENGTH_SHORT).show();
            }
        });

        // 自定义TextView点击事件
        customTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(CustomViewActivity.this, "点击了自定义TextView", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 向FlowLayout添加示例项目
     */
    private void addFlowLayoutItems() {
        String[] items = {"Android", "Java", "Kotlin", "自定义View", "Canvas", "Paint", 
                         "触摸事件", "动画", "性能优化", "ViewGroup", "布局管理", "属性动画"};
        
        for (String item : items) {
            Button button = new Button(this);
            button.setText(item);
            button.setTextSize(12);
            button.setPadding(20, 10, 20, 10);
            
            // 设置随机背景色
            int[] colors = {0xFF2196F3, 0xFF4CAF50, 0xFFFF9800, 0xFF9C27B0, 0xFFF44336, 0xFF607D8B};
            button.setBackgroundColor(colors[(int)(Math.random() * colors.length)]);
            button.setTextColor(0xFFFFFFFF);
            
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(CustomViewActivity.this, "点击了: " + item, Toast.LENGTH_SHORT).show();
                }
            });
            
            flowLayout.addView(button);
        }
    }
}